
-- local on_attach = function(client, bufnr)
--     vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
--     -- 禁用lsp 语义高亮
--     -- client.server_capabilities.semanticTokensProvider = nil
-- end

-- local capabilities = vim.lsp.protocol.make_client_capabilities()
-- capabilities.textDocument.completion.completionItem = {
--     documentationFormat = { "markdown", "plaintext" },
--     -- snippet禁用无效,要在cmp里设置
--     snippetSupport = false,
-- }

return {
    "neovim/nvim-lspconfig",
    dependencies = {
        { "folke/neodev.nvim", version = "*", config = true, lazy = true },
        "hrsh7th/cmp-nvim-lsp",
        { "mason-org/mason-lspconfig.nvim", version = "*", config = true },
    },
    event = { "BufReadPre", "BufNewFile" },
    config = function()
        local capabilities = require('cmp_nvim_lsp').default_capabilities();
        
        -- vim.lsp.config['lua_ls'] = {
        --     -- Command and arguments to start the server.
        --     cmd = { 'lua-language-server' },
        --     -- Filetypes to automatically attach to.
        --     filetypes = { 'lua' },
        --     -- Sets the "workspace" to the directory where any of these files is found.
        --     -- Files that share a root directory will reuse the LSP server connection.
        --     -- Nested lists indicate equal priority, see |vim.lsp.Config|.
        --     root_markers = { { '.luarc.json', '.luarc.jsonc' }, '.git' },
        --     -- Specific settings to send to the server. The schema is server-defined.
        --     -- Example: https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json
        --     settings = {
        --         Lua = {
        --             runtime = {
        --                 version = 'LuaJIT',
        --             }
        --         }
        --     },
        -- };
        -- vim.lsp.enable("lua_ls");
        -- vim.lsp.config["clangd"] = {
        --     cmd = {
        --         "clangd",
        --         -- 同时开启的任务数量
        --         "-j=8",
        --         "--compile-commands-dir=build",
        --         "--clang-tidy",
        --         "--query-driver=/usr/bin/clang++*", 
        --         "--clang-tidy-checks=performance-*,bugprone-*",
        --         "--all-scopes-completion",
        --         "--completion-style=detailed",
        --         "--function-arg-placeholders",
        --         "--header-insertion=iwyu",
        --         "--pch-storage=disk", 
        --     },
        --     filetypes = { "c", "cpp", "hpp", "c++"},
        -- }
        -- vim.lsp.enable("clangd");

        -- 获取 settings 目录路径
        local settings_dir = vim.fn.stdpath("config") .. "/lua/plugins/lsp/settings"
        -- 检查目录是否存在
        if vim.fn.isdirectory(settings_dir) == 1 then
            -- 遍历目录中的所有 .lua 文件
            for _, file in ipairs(vim.fn.readdir(settings_dir, [[v:val =~ '\.lua$']])) do
                local server_name = file:gsub("%.lua$", "")
                local module_path = "plugins.lsp.settings." .. server_name
                local ok, config = pcall(require, module_path)
                
                if ok and type(config) == "table" then
                    -- 直接使用文件名作为服务器名称，文件内容作为配置
                    vim.lsp.config[server_name] = config
                    vim.lsp.enable(server_name)
                else
                    vim.notify("Failed to load LSP config: " .. module_path, vim.log.levels.WARN)
                end
            end
        end

        vim.diagnostic.config({
            virtual_text = {
                severity = vim.diagnostic.severity.ERROR,
                source = "if_many",  -- 仅当存在多个诊断源时显示来源（如 "eslint"、"tsserver"）
            },
        })
    end,
}
